﻿using Coldairarrow.Entity.Backstage.BS_Specials;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Backstage.BS_Specials
{
    public class BS_SpecialBusiness : BaseBusiness<BS_Special>, IBS_SpecialBusiness, ITransientDependency
    {
        public BS_SpecialBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<BS_SpecialDTO>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            input.SortField = "Sort";
            input.SortType = "asc";

            var q = from a in GetIQueryable().Where(x => x.ParentId == null || x.ParentId == "")
                    select new BS_SpecialDTO
                    { 
                       Id = a.Id,
                       CreatorId = a.CreatorId,
                       CreateTime = a.CreateTime,
                       Deleted = a.Deleted,
                       Note = a.Note,
                       ParentId = a.ParentId,
                       Name = a.Name,
                       Sort = a.Sort,
                       ImgURL = a.ImgURL,
                       KeepOne = a.KeepOne,
                       KeepTwo = a.KeepTwo,
                       Child = GetIQueryable().Where(x => x.ParentId.Equals(a.Id)).OrderBy(x =>x.Sort)
                    }


                    ;
            var where = LinqHelper.True<BS_SpecialDTO>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<BS_SpecialDTO, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }


            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<object> GetFontListAsync()
        {
   
            var q = await (from a in GetIQueryable().Where(x => x.ParentId == null || x.ParentId == "")
                    select new BS_SpecialDTO
                    {
                        Id = a.Id,
                        Name = a.Name,
                        Sort = a.Sort,
                        ImgURL = a.ImgURL,
                        KeepOne = a.KeepOne,
                        KeepTwo = a.KeepTwo,
                        Note = a.Note
                    }).OrderBy(x => x.Sort).ToListAsync() ;
          
            return q;
        }


        public async Task<BS_Special> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(BS_Special data)
        {
            int aCount = 0;
            if (data.ParentId.IsNullOrEmpty())
            {
                aCount = GetIQueryable().Where(x => x.Name.Equals(data.Name)).Count();
            }
            else
            {
                aCount = GetIQueryable().Where(x => x.Name.Equals(data.Name) && x.ParentId.Equals(data.ParentId)).Count();
            }

            if (aCount > 0)
            {
                throw new BusException("专题名称不能重复！",-3);
            }

            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(BS_Special data)
        {
            int aCount = 0;
            if (data.ParentId.IsNullOrEmpty())
            {
                aCount = GetIQueryable().Where(x => x.Name.Equals(data.Name) && !x.Id.Equals(data.Id)).Count();
            }
            else
            {
                aCount = GetIQueryable().Where(x => x.Name.Equals(data.Name) && x.ParentId.Equals(data.ParentId) && !x.Id.Equals(data.Id)).Count();
            }

            if (aCount > 0)
            {
                throw new BusException("专题名称不能重复！", -3);
            }
            await UpdateAsync(data);
        }

        /// <summary>
        /// 获取父级列表
        /// </summary>
        /// <returns></returns>
        public List<BS_Special> GetPList()
        {
            var sList = GetIQueryable().Where(x => x.ParentId.IsNullOrEmpty()).ToList();
          
            return sList;
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        #endregion

        #region 私有成员

        #endregion
    }
}